package com.ruoyi.docs.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.docs.domain.BizTreeColumn;
import com.ruoyi.docs.domain.TreeColumnSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.docs.mapper.BizColumnMapper;
import com.ruoyi.docs.domain.BizColumn;
import com.ruoyi.docs.service.IBizColumnService;

/**
 * 栏目Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-05
 */
@Service
public class BizColumnServiceImpl implements IBizColumnService 
{
    @Autowired
    private BizColumnMapper bizColumnMapper;

    /**
     * 查询栏目
     * 
     * @param columnId 栏目主键
     * @return 栏目
     */
    @Override
    public BizColumn selectBizColumnByColumnId(Long columnId)
    {
        return bizColumnMapper.selectBizColumnByColumnId(columnId);
    }

    /**
     * 查询栏目列表
     * 
     * @param bizColumn 栏目
     * @return 栏目
     */
    @Override
    public List<BizColumn> selectBizColumnList(BizColumn bizColumn)
    {
        return bizColumnMapper.selectBizColumnList(bizColumn);
    }

    /**
     * 新增栏目
     * 
     * @param bizColumn 栏目
     * @return 结果
     */
    @Override
    public int insertBizColumn(BizColumn bizColumn)
    {
        bizColumn.setCreateTime(DateUtils.getNowDate());
        return bizColumnMapper.insertBizColumn(bizColumn);
    }

    /**
     * 修改栏目
     * 
     * @param bizColumn 栏目
     * @return 结果
     */
    @Override
    public int updateBizColumn(BizColumn bizColumn)
    {
        bizColumn.setUpdateTime(DateUtils.getNowDate());
        return bizColumnMapper.updateBizColumn(bizColumn);
    }

    /**
     * 批量删除栏目
     * 
     * @param columnIds 需要删除的栏目主键
     * @return 结果
     */
    @Override
    public int deleteBizColumnByColumnIds(Long[] columnIds)
    {
        return bizColumnMapper.deleteBizColumnByColumnIds(columnIds);
    }

    /**
     * 删除栏目信息
     * 
     * @param columnId 栏目主键
     * @return 结果
     */
    @Override
    public int deleteBizColumnByColumnId(Long columnId)
    {
        return bizColumnMapper.deleteBizColumnByColumnId(columnId);
    }

    @Override
    public List<TreeColumnSelect> selectColumnTreeList(Long type) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysRole sysRole = loginUser.getUser().getRoles().get(0);
        System.out.println("loginUser = " + loginUser);
        List<BizTreeColumn> depts = bizColumnMapper.selectColumnList(type);
        if (sysRole.getRoleKey().equals("committee") || sysRole.getRoleKey().equals("business")
                || sysRole.getRoleKey().equals("common")) {
            List<BizTreeColumn> newDepts = new ArrayList<>();
            List<Long> list = Arrays.asList(1l, 11l, 3l, 2l);
            for (BizTreeColumn dept : depts) {
                if (list.contains(dept.getColumnId())) {
                    continue;
                }
                newDepts.add(dept);
            }
            return buildColumnTreeSelect(newDepts);
        }
        return buildColumnTreeSelect(depts);
    }

    private List<TreeColumnSelect> buildColumnTreeSelect(List<BizTreeColumn> depts) {
        List<BizTreeColumn> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeColumnSelect::new).collect(Collectors.toList());
    }
    public List<BizTreeColumn> buildDeptTree(List<BizTreeColumn> depts)
    {
        List<BizTreeColumn> returnList = new ArrayList<BizTreeColumn>();
        List<Long> tempList = depts.stream().map(BizTreeColumn::getColumnId).collect(Collectors.toList());
        for (BizTreeColumn dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<BizTreeColumn> list, BizTreeColumn t)
    {
        // 得到子节点列表
        List<BizTreeColumn> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BizTreeColumn tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BizTreeColumn> getChildList(List<BizTreeColumn> list, BizTreeColumn t)
    {
        List<BizTreeColumn> tlist = new ArrayList<BizTreeColumn>();
        Iterator<BizTreeColumn> it = list.iterator();
        while (it.hasNext())
        {
            BizTreeColumn n = (BizTreeColumn) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getColumnId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BizTreeColumn> list, BizTreeColumn t)
    {
        return getChildList(list, t).size() > 0;
    }
}
